var systemInfo = wx.getSystemInfoSync();
const app = getApp();
Page({
    data: {
        msgList: [],
        scrollY: true,
        page: 1,
        tokenID: '',
        maskShow: false,
        sysidRemove: '',
        eventarget: null,
        classification: false,
        searchType: 1002,
        content: '自定义toast组件',
        iconfont: '',
        isHide: false,
        is_modal_Hidden: true,
        is_modal_Msg: '请输入课程密码',
        passwordValue: '',
        courseLinkUrl: '',
        password:''
    },
    swipeCheckX: 35,
    swipeCheckState: 0,
    maxMoveLeft: 180,
    correctMoveLeft: 130,
    thresholdMoveLeft: 100,
    lastShowMsgId: '',
    moveX: 0,
    showState: 0,
    touchStartState: 0,
    swipeDirection: 0,
    onLoad: function() {
        var _this = this;
        _this.onReachBottom();
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        wx.showLoading({
            title: '玩命加载中',
        })
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + 'optional/getMyOptional',
            data: {
                "page": _this.data.page,
                "token": _this.data.tokenID,
                "type": _this.data.searchType
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        var moment_list = _this.data.msgList;
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            var msg = {};
                            msg.id = 'id-' + i + 1;
                            msg.name = res.data.data.list[i].name;
                            msg.createTime = res.data.data.list[i].createTime;
                            msg.pictureUrl = res.data.data.list[i].pictureUrl;
                            msg.sysid = res.data.data.list[i].sysid;
                            msg.progress = res.data.data.list[i].progress;
                            msg.nice = res.data.data.list[i].nice;
                            msg.viewNum = res.data.data.list[i].viewNum;
                            msg.vid = res.data.data.list[i].url;
                            msg.optionalId = res.data.data.list[i].optionalId;
                            msg.needpwd = res.data.data.list[i].needpwd;
                            msg.password = res.data.data.list[i].password;
                            moment_list.push(msg);
                        }
                        if (res.data.data.list.length == 0) {
                            _this.setData({
                                datamsg: true
                            })
                        }
                        _this.setData({
                            page: _this.data.page + 1,
                            msgList: moment_list
                        })
                    } else {
                        var moment_list = [];
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            var msg = {};
                            msg.id = 'id-' + i + 1;
                            msg.name = res.data.data.list[i].name;
                            msg.createTime = res.data.data.list[i].createTime;
                            msg.pictureUrl = res.data.data.list[i].pictureUrl;
                            msg.sysid = res.data.data.list[i].sysid;
                            msg.progress = res.data.data.list[i].progress;
                            msg.nice = res.data.data.list[i].nice;
                            msg.viewNum = res.data.data.list[i].viewNum;
                            msg.vid = res.data.data.list[i].url;
                            msg.optionalId = res.data.data.list[i].optionalId;
                            msg.needpwd = res.data.data.list[i].needpwd;
                            msg.password = res.data.data.list[i].password;
                            moment_list.push(msg);
                        }
                        _this.setData({
                            page: _this.data.page + 1,
                            msgList: moment_list
                        })
                        wx.stopPullDownRefresh();
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    },
    ConfirmRemove: function() {
        var _this = this;
        wx.request({
            url: app.data.api + 'optional/removeOptional',
            method: "POST",
            data: {
                "token": _this.data.tokenID,
                "oid": _this.data.sysidRemove
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    _this.deleteMsgItem(_this.data.eventarget);
                    _this.layerMsg(true, res.data.msg, 'icon-yiwancheng');
                    _this.setData({
                        maskShow: false
                    })
                } else {
                    _this.layerMsg(true, res.data.msg, 'icon-weiwancheng');
                    _this.setData({
                        maskShow: false
                    })
                }
            }
        })
    },
    cancel: function() {
        this.setData({
            maskShow: false
        })
    },
    showClassification: function() {
        this.setData({
            classification: true
        })
    },
    hideClassification: function() {
        this.setData({
            classification: false
        })
    },
    search: function(event) {
        this.setData({
            searchType: event.currentTarget.dataset.id,
            classification: false,
            page: 1
        })
        this.gtemomentList();
    },
    ontouchstart: function(e) {
        if (this.showState === 1) {
            this.touchStartState = 1;
            this.showState = 0;
            this.moveX = 0;
            this.translateXMsgItem(this.lastShowMsgId, 0, 200);
            this.lastShowMsgId = "";
            return;
        }
        this.firstTouchX = e.touches[0].clientX;
        this.firstTouchY = e.touches[0].clientY;
        if (this.firstTouchX > this.swipeCheckX) {
            this.swipeCheckState = 1;
        }
        this.lastMoveTime = e.timeStamp;
    },
    ontouchmove: function(e) {
        if (this.swipeCheckState === 0) {
            return;
        }
        if (this.touchStartState === 1) {
            return;
        }
        var moveX = e.touches[0].clientX - this.firstTouchX;
        var moveY = e.touches[0].clientY - this.firstTouchY;
        if (this.swipeDirection === 2) {
            return;
        }
        if (this.swipeDirection === 0) {
            if (Math.abs(moveY) > 4) {
                this.swipeDirection = 2;
                return;
            }
            if (Math.abs(moveX) > 4) {
                this.swipeDirection = 1;
                this.setData({
                    scrollY: false
                });
            } else {
                return;
            }
        }
        this.lastMoveTime = e.timeStamp;
        if (moveX > 0) {
            moveX = 0;
        }
        if (moveX < -this.maxMoveLeft) {
            moveX = -this.maxMoveLeft;
        }
        this.moveX = moveX;
        this.translateXMsgItem(e.currentTarget.id, moveX, 0);
    },
    ontouchend: function(e) {
        this.swipeCheckState = 0;
        var swipeDirection = this.swipeDirection;
        this.swipeDirection = 0;
        if (this.touchStartState === 1) {
            this.touchStartState = 0;
            this.setData({
                scrollY: true
            });
            return;
        }
        if (swipeDirection !== 1) {
            return;
        }
        if (this.moveX === 0) {
            this.showState = 0;
            this.setData({
                scrollY: true
            });
            return;
        }
        if (this.moveX === this.correctMoveLeft) {
            this.showState = 1;
            this.lastShowMsgId = e.currentTarget.id;
            return;
        }
        if (this.moveX < -this.thresholdMoveLeft) {
            this.moveX = -this.correctMoveLeft;
            this.showState = 1;
            this.lastShowMsgId = e.currentTarget.id;
        } else {
            this.moveX = 0;
            this.showState = 0;
            this.setData({
                scrollY: true
            });
        }
        this.translateXMsgItem(e.currentTarget.id, this.moveX, 500);
    },
    onDeleteMsgTap: function(e) {
        var optionalId = e.target.dataset.optionalid;
        this.setData({
            maskShow: true,
            sysidRemove: optionalId,
            eventarget: e
        })
    },
    getItemIndex: function(id) {
        var msgList = this.data.msgList;
        for (var i = 0; i < msgList.length; i++) {
            if (msgList[i].id === id) {
                return i;
            }
        }
        return -1;
    },
    deleteMsgItem: function(e) {
        var animation = wx.createAnimation({
            duration: 200
        });
        animation.height(0).opacity(0).step();
        this.animationMsgWrapItem(e.currentTarget.id, animation);
        var s = this;
        setTimeout(function() {
            var index = s.getItemIndex(e.currentTarget.id);
            s.data.msgList.splice(index, 1);
            s.setData({
                msgList: s.data.msgList
            });
        }, 200);
        this.showState = 0;
        this.setData({
            scrollY: true
        });
    },
    translateXMsgItem: function(id, x, duration) {
        var animation = wx.createAnimation({
            duration: duration
        });
        var screenWidth = systemInfo.screenWidth;
        if (screenWidth > 500)
        {
            animation.translateX(x*2).step();
        }else{
            animation.translateX(x).step();
            
        }
        this.animationMsgItem(id, animation);
    },
    animationMsgItem: function(id, animation) {
        var index = this.getItemIndex(id);
        var param = {};
        var indexString = 'msgList[' + index + '].animation';
        param[indexString] = animation.export();
        this.setData(param);
    },
    animationMsgWrapItem: function(id, animation) {
        var index = this.getItemIndex(id);
        var param = {};
        var indexString = 'msgList[' + index + '].wrapAnimation';
        param[indexString] = animation.export();
        this.setData(param);
    },
    layerMsg: function(msgtype, msg, iconfont) {
        let $this = this;
        if (msgtype == true) {
            $this.setData({
                isHide: msgtype,
                content: msg,
                iconfont: iconfont
            })
            setTimeout(function() {
                $this.setData({
                    isHide: false
                })
            }, 2000)
        } else {}
    },
    onMyEvent: function (event) {
        var _this = this;
        this.setData({
            passwordValue: event.detail.passwordValue,
        })
        if (_this.data.passwordValue == _this.data.password) {
            wx.navigateTo({
                url: _this.data.courseLinkUrl
            })
            _this.setData({
                is_modal_Hidden: true,
                password: '',
                courseLinkUrl: ''
            })
        } else {
            wx.showToast({
                title: '课程密码有误！请重新输入。',
                icon: 'none',
                duration: 800
            })
        }
    },
    courseLink(event) {
        var _this = this;
        var url = event.currentTarget.dataset.url;
        var needpwd = event.currentTarget.dataset.needpwd;
        var password = event.currentTarget.dataset.password;
        if (needpwd == 1000) {
            wx.navigateTo({
                url: url
            })
        } else {
            _this.setData({
                is_modal_Hidden: false,
                password: password,
                courseLinkUrl: url
            })
        }
    }
})